mui.init({
	swipeBack:true //右滑关闭功能
});
if(window.plus){
    plusReady();
}else{
    document.addEventListener('plusready',plusReady,false);
}
function plusReady(){
	loaddata();
	document.addEventListener('loadData', loaddata());
}
function loaddata(){
	panUtil.ajaxLoding.start();
    mui.ajax(baseServerAdrr+'/mobile/getYYJGDate',{
	    data: {
	   		starttime:panUtil.DateTool.todayBeforeYear(),
	   		endtime:panUtil.DateTool.today()
	    },
	    dataType:'json',//服务器返回json格式数据
	    type:'post',//HTTP请求类型
	    timeout:30000,//超时时间设置为30秒；
	    success:function(data){
	   	    panUtil.ajaxLoding.stop();
	   	    if(data.code =="success"){
	   	   	    drawline(data.message);
	            drawtable(data.message);
	   	    }else{
	   	   	    mui.toast(data.message);
	   	   	    setTimeout(function(){
		   	    	panUtil.ReLogin();
		   	    },1000);
	   	    }
	    },
	    error:function(xhr,type,errorThrown){
		    //异常处理；
		    mui.toast('服务器异常登录失败! oilPriceLine.js');
	    }
	});
}
var drawline=function(data){
	
	var w = plus.display.resolutionWidth;  //高度
    var h = plus.display.resolutionHeight*0.4;  //宽度
    
    var flow = [];
    var time = [];
    for (var i = 0; i < data.length; i++) {
    	flow.push(data[i].sljg);
    	time.push(data[i].jzrq);
    }
    var pieSize=Math.ceil(data.length / 6);
    var labels = [time[0].replace(/\-/g,"/"),time[pieSize].replace(/\-/g,"/"),time[pieSize*2].replace(/\-/g,"/"),time[pieSize*3].replace(/\-/g,"/"),time[pieSize*4].replace(/\-/g,"/"),time[data.length-1].replace(/\-/g,"/")];
    
    var data = [{
    	name: ' ',
    	value: flow,
    	color: '#ec4646',
    	line_width: 2
    }];
    var chart = new iChart.LineBasic2D({
    	render: 'canvasDiv',
    	data: data,
    	align: 'center',
    	title: {
    		text: '胜利原油现货价格走势',
    		font: '微软雅黑',
    		fontsize: 24,
    		color: '#000000'
    	},
    	subtitle: {
    		text: ' ',
    		font: '微软雅黑',
    		color: '#b4b4b4'
    	},
    	footnote: {
    		text: ' ',
    		font: '微软雅黑',
    		fontsize: 11,
    		fontweight: 600,
    		padding: '0 28',
    		color: '#000000'
    	},
    	width: w,
    	height: h,
    	shadow: false,
    	//shadow_color: '#202020',
    	shadow_blur: 8,
    	shadow_offsetx: 0,
    	shadow_offsety: 0,
    	//background_color: '#2e2e2e',
    	animation: true, //开启过渡动画
    	animation_duration: 600, //600ms完成动画
    	tip: {
    		enable: true,
    		shadow: true,
    		listeners: {
    			//tip:提示框对象、name:数据名称、value:数据值、text:当前文本、i:数据点的索引
    			parseText: function(tip, name, value, text, i) {
    				return "<span style='color:#000000 !implement;font-size:12px;'>" + time[i] + "胜利原油价格<br/>" +
    					"</span><span style='color:#000000;font-size:20px;'>" + value + "(美元/桶)</span>";
    			}
    		}
    	},
    	crosshair: {
    		enable: true,
    		line_color: '#000000'
    	},
    	sub_option: {
    		smooth: true,
    		label: false,
    		hollow: false,
    		hollow_inside: false,
    		point_size: 1
    	},
    	coordinate: {
    		width: w-50,
    		height: h,
    		striped_factor: 0.18,
    		grid_color: '#AFAAAA',
    		axis: {
    			color: '#252525',
    			width: [0, 0, 1, 1]
    		},
    		scale: [{
    			position: 'left',
    			/*start_scale: 0,
    			end_scale: 100,
    			scale_space: 10,
    			scale_size: 2,
    			scale_enable: false,*/
    			label: {
    				color: '#000000',
    				font: '微软雅黑',
    				fontsize: 11,
    				fontweight: 600
    			},
    			scale_color: '#9f9f9f'
    		}, {
    			position: 'bottom',
    			label: {
    				color: '#000000',
    				font: '微软雅黑',
    				fontsize: 11,
    				fontweight: 600
    			},
    			scale_enable: false,
    			labels: labels
    		}]
    	}
    });
    //利用自定义组件构造左侧说明文本
    chart.plugin(new iChart.Custom({
    	drawFn: function() {
    		//计算位置
    		var coo = chart.getCoordinate(),
    			x = coo.get('originx'),
    			y = coo.get('originy'),
    			w = coo.width,
    			h = coo.height;
    		//在左上侧的位置，渲染一个单位的文字
    		chart.target.textAlign('start')
    			.textBaseline('bottom')
    			.textFont('600 11px 微软雅黑')
    			.fillText('原油价格(美元/桶)', x - 20, y - 5, false, '#000000')
    			.textBaseline('top')
    			.fillText('(时间)', x + w-5 , y + h + 18, false, '#000000');
    
    	}
    }));
    //开始画图
    chart.draw();
}

var drawtable=function(data){
	
	var gridHeight=plus.screen.resolutionHeight*0.4;
	var lineNum=Math.floor(gridHeight/26);
	var pan_table_content=document.body.querySelector('.pan_table');
	pan_table_content.innerHTML='';
	var innerhtml="<tr><th width=\"20%\">截至日期</th><th width=\"20%\">迪拜价格</th><th width=\"20%\">胜利价格</th><th width=\"20%\">涨跌额</th><th width=\"20%\">涨跌幅</th></tr>";
	for(var i=(data.length-1);i>=(data.length-lineNum);i--){
		innerhtml+="<tr><td>"+data[i].jzrq+"</td><td>"+data[i].yyjg+"</td><td>"+data[i].sljg+"</td>";
		if(data[i].zde>=0){
			innerhtml+="<td style='color:red;'>"+data[i].zde+"</td><td style='color:red;'>"+data[i].zdf+"</td></tr>";
		}
		if(data[i].zde<0){
			innerhtml+="<td style='color:green;'>"+data[i].zde+"</td><td style='color:green;'>"+data[i].zdf+"</td></tr>";
		}
	}
	pan_table_content.innerHTML=innerhtml;
}
